package net.sk.dc;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.net.InetSocketAddress;
import java.util.HashMap;
import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import net.sk.cam.CamImageFactory;
import net.sk.mina.imagine.FrameHeader;
import net.sk.mina.imagine.ImageRequest;
import net.sk.mina.imagine.ImageResponse;
import net.sk.mina.imagine.codec.ImageCodecFactory;
import net.sk.mina.imagine.server.ImageServerIoHandler;
import net.sk.mina.imagine.server.ImageThread;

import dao.hb.ODevice;

public class IDC_CAM_Robot extends IoHandlerAdapter implements IDC {
	   public static final String INDEX_KEY = ImageServerIoHandler.class.getName() + ".INDEX";
	   private static int index=0;

	static final int CAM_PORT = 33789;
    public static final int CONNECT_TIMEOUT = 3000;
    private SocketConnector connector;
    private IoSession session;
	private ODevice od;
	@Override
	public void open(ODevice od) {
		// TODO Auto-generated method stub
    	this.od = od;
        connector = new NioSocketConnector();
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CamImageFactory(true)));
        connector.setHandler(this);
        connect();
	}

	@Override
	public void close() {
		// TODO Auto-generated method stub
	}

	@Override
	public Object[] get(String[] sns) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object[] getNext(String sn, int length) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void set(HashMap<String, Object> svals) throws Exception {
		// TODO Auto-generated method stub

	}

	
    public void connect() {
        ConnectFuture connectFuture = connector.connect(new InetSocketAddress(od.getIpAddr(), CAM_PORT));
        connectFuture.awaitUninterruptibly(CONNECT_TIMEOUT);
        try {
            session = connectFuture.getSession();
        }
        catch (RuntimeIoException e) {
            //imageListener.onException(e);
            //reconnect();
        }
    }

    public void disconnect() {
        if (session != null) {
            session.close(true).awaitUninterruptibly(CONNECT_TIMEOUT);
            session = null;
        }
    }

    public void sessionOpened(IoSession session) throws Exception {
        //imageListener.sessionOpened();
        session.setAttribute(INDEX_KEY, index++);
        ImageThread qth = new ImageThread(this,session,od);
		Thread th = new Thread(qth);
		th.start();
    }

    public void sessionClosed(IoSession session) throws Exception {
        //imageListener.sessionClosed();
    }

    public void sendRequest(ImageRequest imageRequest) {
        if (session != null) {
             session.write(imageRequest);
        }
    }

    public void messageReceived(IoSession session, Object message) throws Exception {
    }


}
